import { Component, OnInit } from '@angular/core'
import { Router, ActivatedRoute, Params } from '@angular/router'
import { Location } from '@angular/common'
import { AppointDetailService } from './services/appoint-detail.service'
import { CommonUtil } from 'src/app/common/utils/common.util'

@Component({
  selector: 'app-appoint-detail',
  templateUrl: './appoint-detail.component.html',
  styleUrls: ['./appoint-detail.component.scss']
})
export class AppointDetailComponent implements OnInit {
  id = ''
  UpdateTime = ''
  ProjectInfo = {
    Id: '',
    Name: '',
    Photo: '',
    MinArea: 0,
    MaxArea: 0,
    Region: '',
    Landmark: '',
    AveragePrice: 0,
    AroundPrice: 0,
    Unit: '',
    Status: '',
    Property: '',
    Tag: [],
    Notification: {
      Category: '',
      PromotionCategory_Discount: '',
      Title: ''
    }
  }
  comment: any = {
    Id: ''
  }
  error = false
  loading = false

  constructor(
    private router: Router,
    private activatedRoute: ActivatedRoute,
    private location: Location,
    private service: AppointDetailService,
    private utils: CommonUtil
  ) {}

  ngOnInit() {
    this.loading = true
    this.activatedRoute.queryParams.subscribe((params: Params) => {
      this.id = params.id
      this.getDetail(params.id)
      this.getCommentDetail(params.id)
    })
  }

  onClickLeft() {
    this.location.back()
  }
  getDetail(id) {
    this.service.getDetail(
      id,
      res => {
        setTimeout(() => {
          this.error = false
          this.loading = false
        }, 500)
        if (res.Success && res.Result) {
          let item = res.Result.Value[0]
          let projectInfo = item.ProjectInfo
          let Photo = this.utils.getThumbnail(projectInfo.Photo)
          let Tag = this.utils.getTag(projectInfo.Tag)
          for (let i in this.ProjectInfo) {
            if (projectInfo[i]) {
              this.ProjectInfo[i] = projectInfo[i]
            }
          }
          if (projectInfo.Landmark) {
            this.ProjectInfo.Landmark = JSON.parse(projectInfo.Landmark)[0]
          }
          this.ProjectInfo.Photo = Photo
          this.ProjectInfo.Unit = projectInfo.UnitInfo.Name
          this.UpdateTime = item.UpdateTime
          this.ProjectInfo.Tag = Tag
          this.ProjectInfo.Status = projectInfo.StatusInfo.Code
        }
      },
      () => {
        setTimeout(() => {
          this.error = true
          this.loading = false
        }, 500)
      }
    )
  }
  getCommentDetail(indent) {
    this.service.getCommentDetail(indent, res => {
      if (res.Success && res.Result.Value.length > 0) {
        let data = res.Result.Value[0]
        let comment = {
          Id: data.Id,
          Photo: this.utils.getThumbnail(data.IntentInfo.WorkerInfo.Photo),
          Name: data.IntentInfo.WorkerInfo.Name,
          Score: Number(data.Service) / 20,
          Content: data.Comment,
          UpdateTime: data.UpdateTime
        }
        this.comment = comment
      }
    })
  }
  handleReload() {
    this.error = false
    this.loading = true
    this.getDetail(this.id)
  }
}
